package com.mindata.blockchain;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

/**
 * @author wuweifeng on 2018/3/13.
 */
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
	private static ApplicationContext context;

	public static ApplicationContext getApplicationContext() {
		return ApplicationContextProvider.context;
	}

	@Override
	public void setApplicationContext(final ApplicationContext ac) throws BeansException {
		ApplicationContextProvider.context = ac;
	}

	public static <T> T getBean(final Class<T> tClass) {
		return ApplicationContextProvider.context.getBean(tClass);
	}

	public static <T> T getBean(final String name, final Class<T> tClass) {
		return ApplicationContextProvider.context.getBean(name, tClass);
	}

	public static void publishEvent(final ApplicationEvent event) {
		ApplicationContextProvider.context.publishEvent(event);
	}
}
